<template>
  <table width="100%" class="cartTable" cellspacing="0" cellpadding="10px">
    <!-- 首行 -->
    <tr class="title">
      <td width="50">
        <input
          type="checkbox"
          name=""
          class="choose_all"
          :checked="isAll"
          @click="changeAll"
        />
      </td>
      <td width="100" class="">
        <span style="margin-left: -10px">全选</span>
      </td>
      <td width="400">商品信息</td>
      <td width="150">单价（元）</td>
      <td width="150">数量</td>
      <td width="150">金额（元）</td>
      <td width="80">操作</td>
    </tr>

    <!-- 商品具体信息 -->
    <tr class="row" v-for="item in list" :key="item.id">
      <td>
        <input
          v-model="item.isSelected"
          type="checkbox"
          name=""
          class="choose"
        />
      </td>
      <td class="tal">
        <a href="#" class="pic"><img :src="item.img" alt="" /></a>
      </td>
      <td class="tal">
        <a href="#">{{ item.name }}</a>
      </td>
      <td>
        <del class="tdl">￥{{ item.oldPrice }}</del>
        <br />
        <b class="unit">￥{{ item.price }}</b>
      </td>
      <td>
        <div class="amount">
          <a href="#" class="Increase" @click="addList(item.id)">+</a>
          <input type="text" :value="item.count" class="unum" />
          <a href="#" class="Reduce" @click="subList(item.id)">-</a>
        </div>
      </td>
      <td>
        <b class="u-price">￥{{ item.price * item.count }}</b>
      </td>
      <td><a href="#" class="btn-del" @click="delList(item.id)">删除</a></td>
    </tr>
    <tr class="count">
      <td colspan="7">
        <div class="jiesuan clearfix">
          <div class="left fl">
            <p>
              <input
                type="checkbox"
                name=""
                class="choose_all"
                :checked="isAll"
                @click="changeAll"
              />
              全选&nbsp;&nbsp;&nbsp;<a
                href="#"
                class="del_check"
                @click="delAll"
                v-show="showAll"
                >删除选中商品</a
              >
            </p>
          </div>
          <div class="right fr clearfix" v-show="list.length ? true : false">
            <p>商品总计：￥{{ totalPrice }}</p>
            <p>活动优惠：-￥{{ discountPrice }}</p>
            <p>
              已选商品<span class="red t-number">{{ totalCount }} </span>件
              总价(不含运费、税费)：<span class="red t-price"
                >￥{{ totalPrice - discountPrice }}</span
              >
            </p>
            <!-- <div><a href="#" class="btn pay">结算</a></div> -->
            <a href="#" class="btn pay fr">结算</a>
          </div>
        </div>
      </td>
    </tr>
  </table>
</template>

<script>
export default {
  name: 'MyTable',
  props: {
    list: {
      type: Array,
      required: true,
    },
    isAll: {
      type: Boolean,
      required: true,
    },
    showAll: {
      type: Boolean,
      required: true,
    },
  },
  methods: {
    //1.全选功能
    changeAll() {
      this.$emit('changeAll', !this.isAll);
    },
    //3.1加加
    addList(id) {
      this.$emit('countChange', id);
      //this.list.find((item) => item.id === id).count++;
    },
    //3.2减减
    subList(id) {
      this.$emit('countChanges', id);
    },
    //4.删除
    delList(id) {
      this.$emit('delChange', id);
    },
    //删除已选中
    delAll() {
      this.$emit('delAll');
    },
  },
  computed: {
    //2.1.计算商品的价格
    totalPrice() {
      return this.list
        .filter((item) => item.isSelected)
        .reduce((prev, item) => {
          return prev + item.price * item.count;
        }, 0);
    },
    //2.2 活动优惠
    discountPrice() {
      return this.list
        .filter((item) => item.isSelected)
        .reduce((prev, item) => {
          return prev + (item.oldPrice - item.price) * item.count;
        }, 0);
    },
    // 2.3已选商品几件
    totalCount() {
      return this.list
        .filter((item) => item.isSelected)
        .reduce((prev, item) => {
          return prev + item.count;
        }, 0);
    },
  },
};
</script>

<style scoped lang="less"></style>
